package psk.lightshow;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class ModelDot extends Activity {
	private Activity nowActivity = ModelDot.this;
	private TextView tvShowDot;
	private RelativeLayout rlDot;
	private int r=255,g=255,b=255,width=0,size=0;
	private Bitmap bitmap;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.model_dot);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.head_back_title);
		initHead();
		initView();
	}
	private void initHead(){
		TextView tvTitle = (TextView)findViewById(R.id.tvTitle);
        tvTitle.setText("画点");
        Button btnBack = (Button)findViewById(R.id.btnBack);
        btnBack.setOnClickListener(new OnClickListener(){
        	public void onClick(View v){
        		nowActivity.finish();
        	}
        });
	}
	private void initView(){
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		width = dm.widthPixels;
		rlDot = (RelativeLayout)findViewById(R.id.rlDot);
		tvShowDot = (TextView)findViewById(R.id.tvShowDot);
		BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(R.drawable.icon_color_long);
		bitmap = bd.getBitmap();
		Intent it = this.getIntent();
		Bundle bl = it.getExtras();
		changeColor(bl.getInt("position"));
		//Set the seek bar to change the dot's color----------------------------------------------
		SeekBar sbChangeColor = (SeekBar)findViewById(R.id.sbChangeColor);
		sbChangeColor.setMax(bitmap.getWidth()-1);
		sbChangeColor.setProgress(bl.getInt("position"));
		sbChangeColor.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				changeColor(progress);
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});
		//Set the seek bar to change the dot's size-------------------------------------------------
		SeekBar sbSizeChange = (SeekBar)findViewById(R.id.sbSizeChange);
		sbSizeChange.setMax((width/10)-1);
		sbSizeChange.setProgress(40);
		changeSize(40);
		sbSizeChange.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				changeSize(progress);
			}
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});
		//Set the button of btnToShow to start show activity---------------------------------------
		Button btnToShow = (Button)findViewById(R.id.btnToShow);
		btnToShow.setOnClickListener(new OnClickListener(){
			public void onClick(View v){
				Intent it = new Intent(nowActivity,Show.class);
				Bundle bl = new Bundle();
				bl.putString("type", "dot");
				bl.putInt("size", size);
				bl.putInt("r", r);
				bl.putInt("g", g);
				bl.putInt("b", b);
				it.putExtras(bl);
				startActivity(it);
			}
		});
	}
	/**
	 * Change the color of the dot------------------------------------------------------------------------
	 * @param progress-> the position of seek bar
	 */
	private void changeColor(int progress){
		int pixel = bitmap.getPixel(progress, 5);
		r = Color.red(pixel);
		g = Color.green(pixel);
		b = Color.blue(pixel);
		tvShowDot.setBackgroundColor(Color.rgb(r, g, b));
	}
	/**
	 * Change the size of the dot---------------------------------------------------------------------
	 * @param progress-> the position of seek bar
	 */
	private void changeSize(int progress){
		RelativeLayout.LayoutParams loParams = (RelativeLayout.LayoutParams)rlDot.getLayoutParams();
		loParams.width = progress*10+10;
		loParams.height = progress*10+10;
		size = progress*10+10;
		rlDot.setLayoutParams(loParams);
	}

}
